package com.xfc.web.controller.school;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xfc.common.annotation.Log;
import com.xfc.common.constant.Constants;
import com.xfc.common.core.controller.BaseController;
import com.xfc.common.core.domain.AjaxResult;
import com.xfc.common.core.domain.entity.SysUser;
import com.xfc.common.core.page.TableDataInfo;
import com.xfc.common.enums.BusinessType;
import com.xfc.common.utils.poi.ExcelUtil;
import com.xfc.message.NoticeMessage;
import com.xfc.school.domain.ScAuditionClassInfo;
import com.xfc.school.domain.ScTeacher;
import com.xfc.school.service.IScAuditionClassInfoService;
import com.xfc.school.service.IScTeacherService;
import com.xfc.system.service.ISysRoleService;
import com.xfc.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 试听课记录Controller
 * 
 * @author xfc
 * @date 2021-12-19
 */
@RestController
@RequestMapping("/school/audition")
public class ScAuditionClassInfoController extends BaseController
{
    @Autowired
    private IScAuditionClassInfoService scAuditionClassInfoService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private NoticeMessage noticeMessage;

    @Autowired
    private IScTeacherService teacherService;


    /**
     * 查询试听课记录列表
     */
    @PreAuthorize("@ss.hasPermi('school:audition:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScAuditionClassInfo scAuditionClassInfo,Authentication authentication)
    {
        //获取登录用户信息
        SysUser user=userService.selectUserByUserName(authentication.getName());
        //查询用户角色
        String roleCode=sysRoleService.getUerRole(String.valueOf(user.getUserId()));
        if (!Objects.equals("admin",user.getUserName())){
            //判断用户是否是管理员
            if (!Objects.equals(Constants.USER_ROLE,roleCode)){
                scAuditionClassInfo.setTeacherNo(String.valueOf(user.getUserId()));
            }
        }
        startPage();
        List<ScAuditionClassInfo> list = scAuditionClassInfoService.list(Wrappers.query(scAuditionClassInfo));
        return getDataTable(list);
    }

    /**
     * 导出试听课记录列表
     */
    @PreAuthorize("@ss.hasPermi('school:audition:export')")
    @Log(title = "试听课记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ScAuditionClassInfo scAuditionClassInfo)
    {
        List<ScAuditionClassInfo> list = scAuditionClassInfoService.list();
        ExcelUtil<ScAuditionClassInfo> util = new ExcelUtil<ScAuditionClassInfo>(ScAuditionClassInfo.class);
        return util.exportExcel(list, "audition");
    }

    /**
     * 获取试听课记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('school:audition:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        ScAuditionClassInfo auditionClassInfo=scAuditionClassInfoService.getById(id);
        if (Objects.nonNull(auditionClassInfo)){
            ScTeacher teacher=teacherService.getById(auditionClassInfo.getTeacherNo());
            if (Objects.nonNull(teacher)){
                auditionClassInfo.setTeacherNo(teacher.getTeacherName());
            }
        }
        return AjaxResult.success(auditionClassInfo);

    }

    /**
     * 新增试听课记录
     */
    @PreAuthorize("@ss.hasPermi('school:audition:add')")
    @Log(title = "试听课记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScAuditionClassInfo scAuditionClassInfo)
    {
        return toAjax(scAuditionClassInfoService.save(scAuditionClassInfo));
    }

    /**
     * 修改试听课记录
     */
    @PreAuthorize("@ss.hasPermi('school:audition:edit')")
    @Log(title = "试听课记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScAuditionClassInfo scAuditionClassInfo)
    {
        boolean res=scAuditionClassInfoService.updateById(scAuditionClassInfo);
        if (res){
            //根据id查询
            ScAuditionClassInfo scAuditionClassInfo1=scAuditionClassInfoService.getById(scAuditionClassInfo.getId());
            if (Objects.nonNull(scAuditionClassInfo1)&&scAuditionClassInfo.getStatus()==1){
                ScTeacher teacher=teacherService.getById(scAuditionClassInfo.getTeacherNo());
                if (Objects.nonNull(teacher)){
                    //推送信息给家长
                    noticeMessage.appointmentMessage(scAuditionClassInfo1.getApplicantOpenid(),scAuditionClassInfo1.getApplicant(),scAuditionClassInfo1.getCourseName(),"1", DateUtil.format(scAuditionClassInfo.getStartTime(), "yyyy-MM-dd"),"上海市奉贤区南桥镇环城南路1070号207室");
                }
           }
        }
        return toAjax(res);
    }

    /**
     * 删除试听课记录
     */
    @PreAuthorize("@ss.hasPermi('school:audition:remove')")
    @Log(title = "试听课记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids){
		List<String> list=new ArrayList<>();
    	if (ids!=null&&ids.length>0) {
            for (String id : ids) {
                list.add(id);
            }
		}
        return toAjax(scAuditionClassInfoService.removeByIds(list));
    }
}
